// components/deliveryTime/deliveryTime.js
import util from "../../utils/util"
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    distributionType: {
      type: Boolean,
      value: true,
    },
    shippingConfigs: {
      type: Object,
      value: {}
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    time: [],
    otherTime: [],
    date: [{ title: '今天', date: '' }, { title: '明天', date: '' }, { title: '后天', date: '' }, { title: '', date: '' }, { title: '', date: '' }, { title: '', date: '' }, { title: '', date: '' }],
    dateIdx: 0,
    timeIdx: '-1',
    currentDate: '',
    deliveryTimeType: false,
    dateForm: [],
    shippingConfig: { "workBeginTime": "00:15:00", "workEndTime": "23:59:00", "speed": [{ "reach": ["00:00:00", "00:00:00"], "reachDay": 0, "reachTime": 15 }, { "reach": ["00:00:00", "00:00:00"], "reachDay": 0, "reachTime": 15 }], "shippingTime": 15, "shippingmake": 7 },
    estimateDate: '',
    nowType: false,
    detaTitle: ''
  },
  attached: function () {
    var that = this;
    that.data.dateForm = this.getDates(7, new Date())
    that.data.date[3].title = that.data.dateForm[3].day + '号'
    that.data.date[4].title = that.data.dateForm[4].day + '号'
    that.data.date[5].title = that.data.dateForm[5].day + '号'
    that.data.date[6].title = that.data.dateForm[6].day + '号'
    that.data.date[0].date = that.data.dateForm[0].year + '-' + that.data.dateForm[0].month + '-' + that.data.dateForm[0].day
    that.data.date[1].date = that.data.dateForm[1].year + '-' + that.data.dateForm[1].month + '-' + that.data.dateForm[1].day
    that.data.date[2].date = that.data.dateForm[2].year + '-' + that.data.dateForm[2].month + '-' + that.data.dateForm[2].day
    that.data.date[3].date = that.data.dateForm[3].year + '-' + that.data.dateForm[3].month + '-' + that.data.dateForm[3].day
    that.data.date[4].date = that.data.dateForm[4].year + '-' + that.data.dateForm[4].month + '-' + that.data.dateForm[4].day
    that.data.date[5].date = that.data.dateForm[5].year + '-' + that.data.dateForm[5].month + '-' + that.data.dateForm[5].day
    that.data.date[6].date = that.data.dateForm[6].year + '-' + that.data.dateForm[6].month + '-' + that.data.dateForm[6].day
    that.data.currentDate = that.data.date[0].date
    that.setData({
      date: that.data.date,
      currentDate: that.data.currentDate
    })
    console.log(this.data.date)
    console.log(this.data.currentDate)
  },
  observers: {
    'distributionType': function (distributionType) {
      // 在 numberA 或者 numberB 被设置时，执行这个函数
      console.log(this.data.distributionType)
      this.getNowTime()
    },
  },
  /**
   * 组件的方法列表
   */
  methods: {
    /*格式化分钟和小时*/
    withZero(params) {
      return params < 10 ? '0' + params : '' + params;
    },
    /*  数字循环*/
    getLoopArray(start, end) {
      let array = [];
      start = start || 0;
      end = end || 1;
      for (let i = start; i <= end; i++) {
        array.push(this.withZero(i));
      }
      return array;
    },
    getNowTime() {
      let that = this;
      that.data.time = []
      that.data.otherTime = []
      that.dateClick()
      var timestamp = Date.parse(new Date());
      let date = 0
      console.info("shippingConfigs**************************************")
      console.info(that.data.shippingConfigs)
      console.info(that.data.shippingConfigs.shippingTime)
      if (that.data.distributionType == false) {
        if(that.data.shippingConfigs.shippingTime){
          date = new Date(timestamp +=  that.data.shippingConfigs.shippingTime*60 * 1000+15 * 60 * 1000);
        }else{
          date = new Date(timestamp += 30 * 60 * 1000);
        }
      } else {
        if(that.data.shippingConfigs.shippingTime){
          date = new Date(timestamp +=  that.data.shippingConfigs.shippingTime*60 * 1000 + 30 * 60 * 1000);
        }else{
          date = new Date(timestamp += 60 * 60 * 1000);
        }
      }
      let year = date.getFullYear();
      let month = (date.getMonth() + 1 < 10 ? `0${date.getMonth() + 1}` : date.getMonth() + 1);
      let time = date.getDate() < 10 ? `0${date.getDate()}` : date.getDate();
      let hour = date.getHours() < 10 ? `0${date.getHours()}` : date.getHours();
      let minute = date.getMinutes() < 10 ? `0${date.getMinutes()}` : date.getMinutes();
      console.log(minute)
      console.log(`${year}-${month}-${time} ${hour}:${minute}`)
      let parentTime = { date: `${year}-${month}-${time}`, startTime: `${hour}:${minute}` };
      that.data.detaTitle = `${month}-${time}`
      that.data.estimateDate = `${hour}:${minute}`
      that.triggerEvent('myevent', parentTime) //myevent 自定义名称事件，父组件中使用
      let arr = [], hourArr = [], lastTime = [], arrForm = []
      //获取符合要求的分钟时段
      let timeArr = this.getLoopArray(0, 59).filter(x => {
        return x % 10 == 0;
      })
      //获取一天当中符合要求的所有时间段
      for (let i = 0; i < this.getLoopArray(0, 23).length; i++) {
        for (let j = 0; j < timeArr.length; j++) {
          arr.push(`${this.getLoopArray(0, 23)[i]}:${timeArr[j]}`)
        }
      }
      that.data.otherTime = arr
      // console.log(`${hour}:${minute}`)
      //根据当前分钟来判断当前所处的时间段
      let tempTime
      if (minute >= 0 && minute < 10) {
        tempTime = `${hour}:00`
      } else if (minute >= 10 && minute < 20) {
        tempTime = `${hour}:10`
      } else if (minute >= 20 && minute < 30) {
        tempTime = `${hour}:20`
      } else if (minute >= 30 && minute < 40) {
        tempTime = `${hour}:30`
      } else if (minute >= 40 && minute < 50) {
        tempTime = `${hour}:40`
      } else if (minute >= 50 && minute < 60) {
        tempTime = `${hour}:50`
      }
      //获取今天剩余时间符合要求的时间段
      // that.data.time = arr.slice(arr.indexOf(tempTime) + 1)
      that.todayClick(arr,tempTime)
      that.otherDayClick(that.data.otherTime)
      that.setData({
        estimateDate: that.data.estimateDate,
        detaTitle: that.data.detaTitle
        // time: this.data.time,//今天的数据
        // otherTime: that.data.otherTime//明天和后天的时间段数据
      })
    },
    toggleDate(e) {
      let that = this
      if (that.data.dateIdx == e.currentTarget.dataset.index) {
        return false
      } else {
        that.data.timeIdx = '-1'
        that.setData({
          dateIdx: e.currentTarget.dataset.index,
          currentDate: e.currentTarget.dataset.item,
          timeIdx: that.data.timeIdx
        })
      }
    },
    toggleTime(e) {
      let that = this
      that.setData({
        timeIdx: e.currentTarget.dataset.index,
        currentTime: e.currentTarget.dataset.item,
        startTime: e.currentTarget.dataset.item
      })
      let parentTime = { date: that.data.currentDate, startTime: that.data.startTime };
      that.nowDateClick(that.data.startTime)
      that.triggerEvent('myevent', parentTime) //myevent 自定义名称事件，父组件中使用
      that.deliveryTimeClick()
    },
    deliveryTimeClick() {
      this.data.deliveryTimeType = !this.data.deliveryTimeType
      this.setData({
        deliveryTimeType: this.data.deliveryTimeType
      })
    },


    getDates(days, todate = this.getCurrentMonthFirst()) {//todate默认参数是当前日期，可以传入对应时间
      var dateArry = [];
      for (var i = 0; i < days; i++) {
        var dateObj = this.dateLater(todate, i);
        dateArry.push(dateObj)
      }
      console.log(dateArry)
      return dateArry;
    },
    dateLater(dates, later) {
      let dateObj = {};
      let show_day = new Array('周日', '周一', '周二', '周三', '周四', '周五', '周六');
      let date = new Date(dates);
      date.setDate(date.getDate() + later);
      let day = date.getDay();
      dateObj.year = date.getFullYear();
      dateObj.month = ((date.getMonth() + 1) < 10 ? ("0" + (date.getMonth() + 1)) : date.getMonth() + 1);
      dateObj.day = (date.getDate() < 10 ? ("0" + date.getDate()) : date.getDate());
      dateObj.week = show_day[day];
      console.log(dateObj)
      console.log('日期1')
      return dateObj;
    },
    getCurrentMonthFirst() {
      var date = new Date();
      var todate = date.getFullYear() + "-" + ((date.getMonth() + 1) < 10 ? ("0" + (date.getMonth() + 1)) : date.getMonth() + 1) + "-" + (date.getDate() < 10 ? ("0" + date.getDate()) : date.getDate());
      console.log(todate)
      console.log('日期2')
      return todate;
    },
    todayClick(data, tempTime) {
      var workBeginTimeData = this.data.shippingConfig.workBeginTime.split(':')
      var workBeginTime = this.filterDate(workBeginTimeData)
      var workEndTimeData = this.data.shippingConfig.workEndTime.split(':')
      var workEndTime = this.filterDate(workEndTimeData)
      console.log(data)
      console.log(tempTime)
      if (workBeginTimeData[0] > workEndTimeData[0]) {
        var timeTwo = (data.slice(data.indexOf(workBeginTime))).concat(data.slice(0,data.indexOf(workEndTime)+1))
        var time = timeTwo.slice(timeTwo.indexOf(tempTime) + 1)
        if (time.indexOf(workBeginTime) == -1) {
          this.data.time = time.slice(0, time.indexOf(workEndTime) + 1)
        } else {
          this.data.time = time.slice(time.indexOf(workBeginTime), time.indexOf(workEndTime) + 1)
        }
      }else if(workBeginTimeData[0] == workEndTimeData[0] && workBeginTimeData[1] > workEndTimeData[1]){
        var timeTwo = (data.slice(data.indexOf(workBeginTime))).concat(data.slice(0,data.indexOf(workEndTime)+1))
        var time = timeTwo.slice(timeTwo.indexOf(tempTime) + 1)
        if (time.indexOf(workBeginTime) == -1) {
          this.data.time = time.slice(0, time.indexOf(workEndTime) + 1)
        } else {
          this.data.time = time.slice(time.indexOf(workBeginTime), time.indexOf(workEndTime) + 1)
        }
      } else {
        var time = data.slice(data.indexOf(tempTime) + 1)
        if (time.indexOf(workBeginTime) == -1) {
          this.data.time = time.slice(0, time.indexOf(workEndTime) + 1)
        } else {
          this.data.time = time.slice(time.indexOf(workBeginTime), time.indexOf(workEndTime) + 1)
        }
      }
      this.setData({
        time: this.data.time
      })
    },
    otherDayClick(data) {
      console.log(data)
      var workBeginTimeData = this.data.shippingConfig.workBeginTime.split(':')
      var workBeginTime = this.filterDate(workBeginTimeData)
      var workEndTimeData = this.data.shippingConfig.workEndTime.split(':')
      var workEndTime = this.filterDate(workEndTimeData)
      if (workBeginTimeData[0] > workEndTimeData[0]) {
        this.data.otherTime = (data.slice(data.indexOf(workBeginTime))).concat(data.slice(0, data.indexOf(workEndTime) + 1))
      }else if(workBeginTimeData[0] == workEndTimeData[0] && workBeginTimeData[1] > workEndTimeData[1]){
        this.data.otherTime = (data.slice(data.indexOf(workBeginTime))).concat(data.slice(0, data.indexOf(workEndTime) + 1))
      } else {
        this.data.otherTime = data.slice(data.indexOf(workBeginTime), data.indexOf(workEndTime) + 1)
      }
      this.startClick()
      this.setData({
        otherTime: this.data.otherTime
      })
    },
    filterDate(data) {
      var minute = data[1]
      var hour = data[0]
      if (minute >= 0 && minute < 10) {
        return `${hour}:00`
      } else if (minute >= 10 && minute < 20) {
        return `${hour}:10`
      } else if (minute >= 20 && minute < 30) {
        return `${hour}:20`
      } else if (minute >= 30 && minute < 40) {
        return `${hour}:30`
      } else if (minute >= 40 && minute < 50) {
        return `${hour}:40`
      } else if (minute >= 50 && minute < 60) {
        return `${hour}:50`
      }
    },
    swiperChoose(data) {
      console.log(data)
      var index = data.detail.current
      this.data.timeIdx = '-1'
      this.setData({
        dateIdx: index,
        currentDate: this.data.date[index].date,
        timeIdx: this.data.timeIdx
      })
    },
    dateClick() {
      var _this = this
      if (!util.isNull(_this.data.shippingConfigs)) {
        if (!util.isNull(_this.data.shippingConfigs.workBeginTime)) {
          _this.data.shippingConfig.workBeginTime = _this.data.shippingConfigs.workBeginTime
        }
        if (!util.isNull(_this.data.shippingConfigs.workEndTime)) {
          _this.data.shippingConfig.workEndTime = _this.data.shippingConfigs.workEndTime
        }
        if (!util.isNull(_this.data.shippingConfigs.shippingmake)) {
          _this.data.shippingConfig.shippingmake = _this.data.shippingConfigs.shippingmake
          _this.setData({
            shippingConfig: _this.data.shippingConfig
          })
        }
      }
    },
    startClick() {
      var timestamp = Date.parse(new Date());
      let date = 0
      date = new Date(timestamp);
      let year = date.getFullYear();
      let month = (date.getMonth() + 1 < 10 ? `0${date.getMonth() + 1}` : date.getMonth() + 1);
      let time = date.getDate() < 10 ? `0${date.getDate()}` : date.getDate();
      let hour = date.getHours() < 10 ? `0${date.getHours()}` : date.getHours();
      let minute = date.getMinutes() < 10 ? `0${date.getMinutes()}` : date.getMinutes();
      var data = `${hour}:${minute}`
      console.log(data)
      this.nowDateClick(data)
    },
    nowDateClick(data) {
      var that = this
      var nowDate = this.filterDate(data.split(':'))
      console.log(nowDate)
      console.log("选择时间")
      if (this.data.otherTime.indexOf(nowDate) == -1) {
        this.data.nowType = true
      } else {
        this.data.nowType = false
      }
      this.triggerEvent('nowTypeClick', this.data.nowType);
      this.setData({
        nowType: this.data.nowType
      })
    }
  }
})
